
local Net = {};

local __net__ = LuaGui.Net;
local __all_socket_map__ = {};
local Socket = commonlib.inherit(commonlib.ToolBase);
Socket:Property("UID", 0);
Socket:Property("Valid", false, "IsValid");
Socket:Property("Server");

function Socket:ctor()
end

function Socket:Init()
    return self;
end

function Socket:Send(data)
    if (not self:IsValid() or not self.__socket__) then return end 
    self.__socket__:Send(data);
end

function Socket:Connect(ip, port)
    self.__socket__ = __net__.ConnectServer(ip, port);
    self:SetUID(self.__socket__:GetUID());
    __all_socket_map__[self:GetUID()] = self;
    return self;
end

function Socket:Close()
    self:SetValid(false);
    if (self.__socket__) then self.__socket__:Close() end 
end

function Socket:OnRecv(data)
    local server = self:GetServer();
    
    -- 默认无处理直接关闭套接字
    if (not server) then 
        self:Close();
        return;
    end

    server:Handle(self, data);
end

function Socket:OnConnection()
    self:SetValid(true);
end

function Socket:OnDisconnection()
    self:SetValid(false);
end

function DefaultGetSocketBySocketUID(socketuid)
    local socket = __all_socket_map__[socketuid];
    if (socket) then return socket end
    socket = Socket:new():Init();
    socket:SetUID(socketuid);
    __all_socket_map__[socketuid] = socket;
    return socket;
end

local Server = commonlib.inherit(commonlib.ToolBase);
Server:Property("Ip", "0.0.0.0");
Server:Property("Port", 80);
Server:Property("UID", 0);

local __all_server_map__ = {};

function Server:ctor()
end

function Server:Init()
    return self;
end

function Server:Handle(socket, data)
end

function Server:GetSocketBySocketUID(socketuid)
    return DefaultGetSocketBySocketUID(socketuid);
end

function Server:Start(ip, port)
    self:SetIp(ip);
    self:SetPort(port);
    
    self.__server__ = __net__.StartServer(self:GetIp(), self:GetPort());
    self:SetUID(self.__server__:GetUID());
    print("Start Server: ", self:GetIp(), self:GetPort());
    __all_server_map__[self:GetUID()] = self;
    return self.__server__ and true or false;
end

function Server:Stop()
    if (self.__server__) then
        __net__.StopServer(self.__server__);
    end
    self.__server__ = nil;
    __all_server_map__[self:GetUID()] = nil;
end

_G.__on_net__ = function(cmd, __socket__, data)
    local socketuid = __socket__:GetUID();
    local serveruid = __socket__:GetServerUID();
    local server = __all_server_map__[serveruid];
    local socket = __all_socket_map__[socketuid] or (server and server:GetSocketBySocketUID(socketuid) or DefaultGetSocketBySocketUID(socketuid));
    socket:SetServer(server);    
    if (cmd == "OnNetRecv") then
        -- print("--------------OnNetRecv--------------------", data)
        socket:OnRecv(data);
    elseif (cmd == "OnNetConnection") then
        -- print("--------------OnNetConnection--------------------")
        socket.__socket__ = __socket__;
        socket:SetUID(socketuid);
        socket:OnConnection(socket);
    elseif (cmd == "OnNetDisconnection") then
        socket.__socket__ = nil;
        socket:OnDisconnection(socket);
    else 
        -- print(cmd, socket:GetUID());
    end
end


Net.Socket = Socket;
Net.Server = Server;
commonlib.Net = Net;

return Net;